package com.pai4j.message.builder.system;

import com.pai4j.common.constants.MessageConstants;
import com.pai4j.common.constants.WebConstant;
import com.pai4j.common.enums.InteractionMessageTypeEnum;
import com.pai4j.domain.vo.dto.SystemMessageDTO;
import com.pai4j.message.core.builder.AbstractMessageBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 系统消息构建器
 * 用于构建系统通知消息
 *
 * @author PAI4J Team
 * @date 2025-11-01
 */
@Slf4j
public class SystemMessageBuilder extends AbstractMessageBuilder<SystemMessageDTO> {

    public SystemMessageBuilder() {
        super();
        context.setMessageTypeEnum(InteractionMessageTypeEnum.SYSTEM);
        context.setType(MessageConstants.TypeEnum.OTHER.getType());
        context.setCategory(MessageConstants.CategoryEnum.SYSTEM.getCategory());
        // 系统消息默认发送者为SYSTEM
        context.setSenderId(WebConstant.SYSTEM_USER_ID);
    }

    @Override
    protected String getMessageCategory() {
        return MessageConstants.CategoryEnum.SYSTEM.getCategory();
    }

    @Override
    protected SystemMessageDTO createMessageDTO() {
        return new SystemMessageDTO();
    }

    @Override
    protected void fillMessageDTO(SystemMessageDTO messageDTO) {
        // 填充资源信息
        if (context.getResourceType() != null) {
            messageDTO.setResourceType(context.getResourceType());
        }
        if (context.getResourceId() != null) {
            messageDTO.setResourceId(context.getResourceId());
        }

        // 填充活动信息
        if (StringUtils.isNotBlank(context.getActivityName())) {
            messageDTO.setActivityName(context.getActivityName());
        }
        if (StringUtils.isNotBlank(context.getActivityUrl())) {
            messageDTO.setActivityUrl(context.getActivityUrl());
        }
    }

    @Override
    protected boolean doValidate() {
        // 系统消息内容不能为空
        if (StringUtils.isBlank(context.getContent())) {
            log.warn("系统消息内容不能为空");
            return false;
        }
        return super.doValidate();
    }

    @Override
    protected String[] getTemplateVarNames() {
        return new String[]{"userName", "content", "activityName", "resourceTitle"};
    }

    /**
     * 设置活动信息
     */
    public SystemMessageBuilder activity(String activityName, String activityUrl) {
        context.setActivityName(activityName);
        context.setActivityUrl(activityUrl);
        return this;
    }

    /**
     * 快速构建系统消息
     */
    public static SystemMessageBuilder create() {
        return new SystemMessageBuilder();
    }
}

